package com.my.controller;

import com.my.pojo.Check;
import com.my.service.ICheckService;
import com.my.service.impl.CheckLogService;
import lombok.Data;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "CheckController",value = "/check/check")
@Data
public class CheckController extends HttpServlet {
    private ICheckService checkService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        this.addCheck(req,resp);
    }
    public void addCheck(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        Check check = new Check();
        check.setCheckDesc(request.getParameter("checkDesc"));
        check.setCheckId(1);
        check.setCheckTag(0);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        check.setCheckTime(dateFormat.format(date));
        String ip = request.getLocalAddr();
        HttpSession session = request.getSession();
        session.setAttribute("ip",ip);
        session.setAttribute("check",check);
        ServletContext sc = this.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc);
        CheckLogService checkLogService = (CheckLogService)wac.getBean("checkLogService");
        checkLogService.setRequest(request);
        ICheckService checkService = (ICheckService) wac.getBean("checkProxy");
        checkService.addCheck(check);
        response.sendRedirect("/check/checkinfo.jsp");
    }
}
